package com.dpac.core.shiro.realm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

/**
 * @title shiro的权限认证和授权认证
 * @description 请填写相关描述
 * @creator lils
 * @date 2017年8月17日 下午4:28:10
 */
@Component
public class ShiroSecurityRealm extends AuthorizingRealm {

	public ShiroSecurityRealm() {
		setName("shiroSecurityRealm");
	}

	/**
	 * 1.shiro认证方法(可以实现身份认证) 验证当前登录的subject
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
		// authcToken是从LoginController里面subject.login(token)传递过来
		return null;
	}

	/**
	 * 2.shiro授权方法(可以实现对资源的分配和拦截)
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

		return null;
	}

	/**
	 * 将一些数据放到ShiroSession中,以便于其它地方使用
	 * @see 比如Controller,使用时直接用HttpSession.getAttribute(key)就可以取到
	 */
	@SuppressWarnings("unused")
	private void setSession(Object key, Object value) {
		Subject currentUser = SecurityUtils.getSubject();
		if (currentUser != null) {
			Session session = currentUser.getSession();
			// System.out.println("Session默认超时时间为[" + session.getTimeout() + "]毫秒");
			if (session != null) {
				session.setAttribute(key, value);
			}
		}
	}
}
